package com.example.controller;

import com.example.service.HazelcastService;
import com.hazelcast.crdt.pncounter.PNCounter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/****
 *
 * 分布式计数器
 */
@RestController
public class TestPNCounterController {

    Logger logger = LoggerFactory.getLogger(getClass());

    public static final String PNCounter = "PNCounter_PNCounter_PNCounter_PNCounter";

    @Resource
    private HazelcastService hazelcastService;

    /***
     * 设置计数
     * @return
     */
    @RequestMapping("setPNCounter")
    public long setPNCounter() {
        PNCounter pnCounter = hazelcastService.getPNCounter(PNCounter);
        pnCounter.incrementAndGet();
        return pnCounter.get();
    }

    /***
     * 获取计数
     * @return
     */
    @RequestMapping("getPNCounter")
    public long getPNCounter() {
        PNCounter pnCounter = hazelcastService.getPNCounter(PNCounter);
        return pnCounter.get();
    }
}
